<template>
    <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

const animationDuration = 6000

export default {
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: 'chart',
        },
        width: {
            type: String,
            default: '100%',
        },
        height: {
            type: String,
            default: '300px',
        },
        chartData: {
            type: Array,
            default: [],
        },
        materialName: {
            type: String,
            default: '',
        },
    },
    watch: {
        chartData(val) {
            this.$nextTick(() => {
                this.initChart(val)
            })
        },
    },
    data() {
        return {
            chart: null,
        }
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    methods: {
        initChart(val) {
            this.chart = echarts.init(this.$el, 'macarons')
            let chartData = [...val]
            let names = []
            let passRates = []
            let nums = []
            chartData.forEach((io) => {
                names.push(io.customerName || io.customerCode)
                passRates.push(parseFloat(io.rate).toFixed(0) + '')
                nums.push(io.num + '')
            })
            let _this = this
            let colors = ['#5470C6', '#EC808D', '#95F204']
            let options = {
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true,
                },
                xAxis: {
                    type: 'category',
                    data: names,
                    axisTick: {
                        show: false, //刻度线
                    },
                    axisLabel: {
                        //x轴文字的配置
                        show: true,
                        textStyle: {
                            color: '#A5A5AA',
                        },
                    },
                },
                tooltip: {
                    show: true,
                    backgroundColor: '#fff',
                    trigger: 'axis',
                },
                toolbox: {
                    show: true,
                    feature: {
                        saveAsImage: {
                            show: true,
                        },
                    },
                },
                yAxis: [
                    {
                        name: '数量',
                        type: 'value',
                        splitLine: {
                            show: false,
                        },
                        axisLabel: {
                            formatter: '{value}',
                            textStyle: {
                                color: 'rgba(143, 211, 250, 1)',
                                fontSize: 12,
                            },
                        },
                        axisLine: {
                            lineStyle: {
                                fontSize: 12,
                                color: 'rgba(66, 192, 255, .3)',
                            },
                        },
                    },
                    {
                        name: '不良率',
                        nameTextStyle: {
                            color: 'rgba(143, 211, 250, 1)',
                        },
                        type: 'value',
                        axisLabel: {
                            formatter: '{value}',
                            textStyle: {
                                color: 'rgba(143, 211, 250, 1)',
                                fontSize: 12,
                            },
                        },
                        axisTick: {
                            show: false,
                        },
                        axisLine: {
                            show: false,
                            lineStyle: {
                                color: 'rgba(143, 211, 250, 1)',
                                width: 1,
                                type: 'solid',
                            },
                        },
                        splitLine: {
                            lineStyle: {
                                color: 'rgba(143, 211, 250, .1)',
                                type: '',
                            },
                        },
                    },
                ],
                // dataZoom: [
                //     {
                //         type: 'inside',
                //         start: 0,
                //         end: 10,
                //     },
                //     {
                //         start: 0,
                //         end: 10,
                //     },
                // ],
                series: [
                    {
                        name: '不良数量',
                        data: nums,
                        type: 'bar',
                        barGap: '0',
                        barWidth: '15',
                        // color: "rgba(255,255,255,0.1)",
                        // barGap: '-100%',
                        itemStyle: {
                            color: '#20B0AB',
                        },
                        z: '-1',
                        label: {
                            color: '#20B09F',
                            show: true, //是否显示
                            position: 'top', //显示位置
                        },
                    },
                    {
                        name: '不良PPM',
                        yAxisIndex: 1,
                        data: passRates,
                        type: 'line',
                        color: '#ffa05e',
                        itemStyle: {
                            normal: {
                                label: {
                                    show: true,
                                },
                            },
                        },
                    },
                ],
            }
            this.chart.clear()
            this.chart.setOption(options)
            this.chart.on('click', (params) => {
                let currentParams = this.chartData.find((io) => {
                    return io.code === params.name
                })

                console.log('currentParams', currentParams)
                this.$emit('callback', currentParams)
                // this.currentParams = { name: params.name, type: 1 }
                // this.getModelData(params.name, 1)
            })
        },
    },
}
</script>
